const fs = require("fs")
const path = require("path")

// 特殊组件映射（目录名与组件名不一致的情况）
const SPECIAL_MAPPINGS = {
	ElSkeletonItem: "skeleton-item",
	ElTableColumn: "table-column",
	ElBreadcrumbItem: "breadcrumb-item",
	ElTimePicker: "time-picker",
	ElDatePicker: "date-picker",
	ElRadioButton: "radio-button",
	ElCheckboxButton: "checkbox-button",
	ElMenuItem: "menu-item",
	ElMenuItemGroup: "menu-item-group",
	ElDropdownItem: "dropdown-item",
	ElDropdownMenu: "dropdown-menu",
	ElCarouselItem: "carousel-item",
	ElCollapseItem: "collapse-item",
	ElCascaderPanel: "cascader-panel",
	ElDescriptionsItem: "descriptions-item",
	ElTimelineItem: "timeline-item",
	ElSplitterPanel: "splitter-panel",
	ElTabPane: "tab-pane",
	ElTreeV2: "tree-v2",
	ElTableV2: "table-v2",
	ElSelectV2: "select-v2",
	ElTooltipV2: "tooltip-v2",
	ElImageViewer: "image-viewer",
	ElConfigProvider: "config-provider",
	ElFocusTrap: "focus-trap",
	ElRovingFocusGroup: "roving-focus-group",
	ElVisualHidden: "visual-hidden"
}

// 获取组件目录名
function getComponentDir(componentName) {
	if (SPECIAL_MAPPINGS[componentName]) {
		return SPECIAL_MAPPINGS[componentName]
	}
	// 常规转换：ElButton → button
	return componentName
		.replace(/^El/, "")
		.replace(/([A-Z])/g, "-$1")
		.toLowerCase()
}

// ElementPlus 组件目录
const componentsDir = path.resolve(
	__dirname,
	"../node_modules/element-plus/es/components"
)

// 读取所有组件文件夹
const componentNames = fs
	.readdirSync(componentsDir)
	.filter(dir => {
		const fullPath = path.join(componentsDir, dir)
		return (
			fs.statSync(fullPath).isDirectory() &&
			dir[0] !== "_" && // 排除内部目录
			dir !== "locale" // 排除非组件目录
		)
	})
	.map(dir => {
		// 转换为 PascalCase（如 button → ElButton）
		const pascalName = "El" + dir.charAt(0).toUpperCase() + dir.slice(1)
		return pascalName.replace(/-([a-z])/g, (_, char) => char.toUpperCase())
	})

// 生成映射文件
let content = `// Auto-generated file. DO NOT EDIT.\n\n`
content += `import * as ElementPlus from 'element-plus';\n\n`

// 生成所有组件映射（ElButton → OvButton）
componentNames.forEach(componentName => {
	const ovName = "Ov" + componentName.substring(2) // 移除 El 前缀，添加 Ov 前缀
	content += `export const ${ovName} = ElementPlus.${componentName};\n`
})

fs.writeFileSync(path.resolve(__dirname, "../src/mappings.js"), content, {
	encoding: "utf-8"
})

console.log(`✅ 生成 ${componentNames.length} 个映射组件`)
